package com.kensure.mycom.em.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;
import co.kensure.mem.Utils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.mycom.em.dao.EmployerLoginDao;
import com.kensure.mycom.em.model.Employer;
import com.kensure.mycom.em.model.EmployerLogin;


/**
 * 员工登录表服务实现类
 * @author fankd created on 2019-6-14
 * @since 
 */
@Service
public class EmployerLoginService extends JSBaseService{
	
	@Resource
	private EmployerLoginDao dao;
	@Resource
	private EmployerService employerService;
	@Resource
	private BaseKeyService baseKeyService;
    
    
    public EmployerLogin selectOne(Long id){
    	return dao.selectOne(id);
    }
	
	public List<EmployerLogin> selectByIds(Collection<Long> ids){
		return dao.selectByIds(ids);
	}
	
	public List<EmployerLogin> selectAll(){
		return dao.selectAll();
	}
	
	public List<EmployerLogin> selectByWhere(Map<String, Object> parameters){
		return dao.selectByWhere(parameters);
	}
	
	
	public long selectCount(){
		return dao.selectCount();
	}
	
	public long selectCountByWhere(Map<String, Object> parameters){
		return dao.selectCountByWhere(parameters);
	}
	
	
	public boolean insert(EmployerLogin obj){
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("employer_login"));
		return dao.insert(obj);
	}
	
	public boolean update(EmployerLogin obj){
		return dao.update(obj);
	}
    
    public boolean updateByMap(Map<String, Object> params){
		return dao.updateByMap(params);
	}
    
    
	public boolean delete(Long id){
		return dao.delete(id);
	}	
	
    public boolean deleteMulti(Collection<Long> ids){
		return dao.deleteMulti(ids);
	}
    
    public boolean deleteByWhere(Map<String, Object> parameters){
		return dao.deleteByWhere(parameters);
	}
    
    /**
	 * 登录逻辑
	 * 
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public String login(String loginname,String password) {
		if (StringUtils.isBlank(loginname)) {
			BusinessExceptionUtil.threwException("账户不能为空");
		}
		if (StringUtils.isBlank(password)) {
			BusinessExceptionUtil.threwException("密码不能为空");
		}	
		Employer emuser = employerService.selectByLoginame(loginname);
		if (emuser == null) {
			BusinessExceptionUtil.threwException("用户或者密码错误!");
		}
		if(!password.equalsIgnoreCase(emuser.getPassword())){
			BusinessExceptionUtil.threwException("用户或者密码错误!!");
		}
		EmployerLogin emlogin = new EmployerLogin();
		emlogin.setSessionid(Utils.getUUID());
		emlogin.setUserid(emuser.getId());		
		insert(emlogin);
		return emlogin.getSessionid();
	}
	
	
	/**
	 * 根据令牌拿到用户登录信息 tokenid = sessionid
	 * 
	 * @return
	 */
	public EmployerLogin selectByTokenId(String tokenid) {
		Map<String, Object> parameters = MapUtils.genMap("sessionid", tokenid);
		List<EmployerLogin> loginlist = selectByWhere(parameters);
		EmployerLogin u = null;
		if (CollectionUtils.isNotEmpty(loginlist)) {
			u = loginlist.get(0);
		}
		return u;
	}

}
